package com.bgds.sys.module.controller;


/**
 * @author lj
 */

import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import com.mchange.lang.DoubleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(tags = "轧机辅助监测接口")
@RequestMapping("/millAuxiliary")
public class MillAuxiliaryController {

    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;
    @Autowired
    private InfluxService influxService;
    @Autowired
    private IAlarmLogService iAlarmLogService;

    @RequestMapping(value = "/getRollingMillChartData", method = RequestMethod.GET)
    @ApiOperation(value = "轧机综合图形分析数据", notes = "轧机综合图形分析数据", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", value = "设备编码"),
            @ApiImplicitParam(name = "startTime", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    public Result<Map<String, Object>> getRollingMillChartData(String deviceCode, Long startTime, Long endTime) {
        Map<String, Object> map = new HashMap<>(10);

        List<DeviceDynamicAttr> deviceDynamicAttrs = iDeviceDynamicAttrService.listByDeviceCode(deviceCode, "zj");
        List<AlarmLogDto> alarmLogs = iAlarmLogService.listByParam(deviceCode, startTime, endTime);

        Map<String, List<DeviceDynamicAttr>> groupMap = deviceDynamicAttrs.stream().
                collect(
                        Collectors.groupingBy((DeviceDynamicAttr dda) -> dda.getGroupCode())
                );

        Map<String, List<SeriesVo>> chartObject = new HashMap<>(10);
        List<SeriesVo>  zzlCount=new ArrayList<>();
        List<SeriesVo>  gfCount=new ArrayList<>();
        List<SeriesVo>  zzlcha=new ArrayList<>();
        List<SeriesVo>  gfcha=new ArrayList<>();
        groupMap.forEach((k, v) -> {
            //图表数据
            List<SeriesVo> chartData = influxService.queryByForMap(deviceCode, startTime, endTime, v);
//            if ("roll_force".equals(k)) {
//                for (SeriesVo vo : chartData) {
//                    Double va = vo.getValue() / 2;
//                    vo.setValue(va);
//                }
//            }
            //油柱差
            if("yz".equals(k)){
                //计算差值
                Map<Long,List<SeriesVo>> group= chartData.stream().collect(Collectors.groupingBy(SeriesVo::getTime));
                //差值对象集合
                group.forEach((time,seriesVos)->{
                    SeriesVo s=new SeriesVo();
                    Double diffValue= seriesVos.get(0).getValue()-seriesVos.get(1).getValue();
                    DecimalFormat df = new DecimalFormat("#.00");
                    s.setName("油柱差值");
                    s.setTime(time);
                    s.setValue(Double.valueOf(df.format(Math.abs(diffValue))));
                    gfcha.add(s);
                });
            }
            //辊缝
            if("gf".equals(k)){
                gfcha.addAll(chartData);
                //计算差值
                Map<Long,List<SeriesVo>> group= chartData.stream().collect(Collectors.groupingBy(SeriesVo::getTime));
                //计算总数
                group.forEach((time,seriesVos)->{
                    SeriesVo s1=new SeriesVo();
                    SeriesVo s2=new SeriesVo();
                    Double diffValue1= seriesVos.get(0).getValue()+seriesVos.get(3).getValue();
                    Double diffValue2= seriesVos.get(1).getValue()+seriesVos.get(2).getValue();
                    DecimalFormat df = new DecimalFormat("#.00");
                    s1.setName("设定辊缝");
                    s1.setTime(time);
                    s1.setValue(Double.valueOf(df.format(diffValue1)));
                    s2.setName("反馈辊缝");
                    s2.setTime(time);
                    s2.setValue(Double.valueOf(df.format(diffValue2)));
                    gfCount.add(s1);
                    gfCount.add(s2);
                });

                //差值对象集合
                group.forEach((time,seriesVos)->{
                    SeriesVo s=new SeriesVo();
                    Double diffValue= seriesVos.get(2).getValue()-seriesVos.get(3).getValue();
                    DecimalFormat df = new DecimalFormat("#.00");
                    s.setName("辊缝差");
                    s.setTime(time);
                    s.setValue(Double.valueOf(df.format(diffValue)));
                    gfcha.add(s);
                });
            }
            //轧制力
            if("zzl".equals(k)){
                zzlcha.addAll(chartData);
                //计算差值
                Map<Long,List<SeriesVo>> group= chartData.stream().collect(Collectors.groupingBy(SeriesVo::getTime));
                //计算总数
                group.forEach((time,seriesVos)->{
                    SeriesVo s1=new SeriesVo();
                    SeriesVo s2=new SeriesVo();
                    Double diffValue1= seriesVos.get(0).getValue()+seriesVos.get(1).getValue();
                    Double diffValue2= seriesVos.get(2).getValue()+seriesVos.get(3).getValue();
                    DecimalFormat df = new DecimalFormat("#.00");
                    s1.setName("设定轧制力");
                    s1.setTime(time);
                    s1.setValue(Double.valueOf(df.format(diffValue1)));
                    s2.setName("反馈轧制力");
                    s2.setTime(time);
                    s2.setValue(Double.valueOf(df.format(diffValue2)));
                    zzlCount.add(s1);
                    zzlCount.add(s2);
                });
                //差值对象集合
                group.forEach((time,seriesVos)->{
                    SeriesVo s=new SeriesVo();
                    Double diffValue= seriesVos.get(2).getValue()-seriesVos.get(3).getValue();
                    DecimalFormat df = new DecimalFormat("#.00");
                    s.setName("轧制力差");
                    s.setTime(time);
                    s.setValue(Double.valueOf(df.format(diffValue)));
                    zzlcha.add(s);
                });
            }
            chartObject.put(k, chartData);


        });
        chartObject.put("gfcha",gfcha);
        chartObject.put("gfCount",gfCount);
        chartObject.put("zzlCount",zzlCount);
        chartObject.put("zzlcha",zzlcha);
        //计算差值
        map.put("chartList", chartObject);
        map.put("alarmLogs", alarmLogs);
        return new ResultUtil<Map<String, Object>>().setData(map);
    }


    @RequestMapping(value = "/getPcChartData", method = RequestMethod.GET)
    @ApiOperation(value = "PC轧机正反转测试监测图形分析数据", notes = "PC轧机正反转测试监测图形分析数据", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", value = "设备编码"),
            @ApiImplicitParam(name = "startTime", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    public Result<Map<String, Object>> getPcChartData(String deviceCode,String filterCode, Long startTime, Long endTime) {
        Map<String, Object> map = new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs = iDeviceDynamicAttrService.listByDeviceCode(deviceCode, null);
        //过滤PC轧机类型正反轧制力动态属性     //roll_force
        List<DeviceDynamicAttr> filterList = deviceDynamicAttrs.stream().filter(attr -> filterCode.equals(attr.getGroupCode())).collect(Collectors.toList());
        //设备时间段报警信息
        List<AlarmLogDto> alarmLogs = iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        //图表数据
        List<SeriesVo> list = influxService.queryByForMap(deviceCode, startTime, endTime, filterList);
//        正转轧制力差值=正转操作侧-正转传动侧
//        反转轧制力差值=反转操作侧-传动传动侧
        //轧制力变化率

        map.put("data", list);
        map.put("alarmLogs", alarmLogs);
        return new ResultUtil<Map<String, Object>>().setData(map);
    }

    @RequestMapping(value = "/getChartData", method = RequestMethod.GET)
    @ApiOperation(value = "轧机窜辊监测图形分析数据", notes = "轧机窜辊监测图形分析数据", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", value = "设备编码"),
            @ApiImplicitParam(name = "startTime", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
    })
    /**
     * 1593484965000, 1593503707000,Q1014100010001002-S0146
     */
    public Result<Map<String, Object>> getChartData(String deviceCode, Long startTime, Long endTime) {
        Map<String, Object> map = new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs = iDeviceDynamicAttrService.listByDeviceCode(deviceCode, null);
        //过滤出轧机窜辊类型设定值、反馈值动态属性
        // List<DeviceDynamicAttr>  filterList= deviceDynamicAttrs.stream().filter(attr->"SET_FEED_BACK".equals(attr.getGroupCode()) ).collect(Collectors.toList());
        List<DeviceDynamicAttr> filterList = deviceDynamicAttrs.stream().filter(attr -> "roll_gap".equals(attr.getGroupCode())).collect(Collectors.toList());
        List<SeriesVo> list = influxService.queryByForMap(deviceCode, startTime, endTime, filterList);
        map.put("data", list);
        return new ResultUtil<Map<String, Object>>().setData(map);
    }
}
